<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Convenience class that implements the transport implementation. Can be extended by
 * real implementations to do some of the common book keeping
 */
abstract class Http_Client {

	/**
	 * Our default timeout value for requests that don't specify a timeout
	 *
	 * @var float
	 */
	private $_default_timeout = false;

	/**
	 * Get the current default timeout setting (initially the default_socket_timeout ini setting)
	 * in seconds
	 *
	 * @return float
	 */
	public function get_default_timeout()
	{
		// lazy load the default timeout from the ini settings
		if ($this->_default_timeout === false)
		{
			$this->_default_timeout = (int) ini_get('default_socket_timeout');

			// double check we didn't get 0 for a timeout
			if ($this->_default_timeout <= 0)
			{
				$this->_default_timeout = 60;
			}
		}

		return $this->_default_timeout;
	}

	/**
	 * Set the current default timeout for all HTTP requests
	 *
	 * @param float $timeout
	 */
	public function set_default_timeout($timeout)
	{
		$timeout = (float) $timeout;

		if ($timeout >= 0)
		{
			$this->_default_timeout = $timeout;
		}
	}

	/**
	 * Perform a GET HTTP operation with an optional timeout and return the response
	 * contents, use getLastResponseHeaders to retrieve HTTP headers
	 *
	 * @param string $url
	 * @param float $timeout
	 * @return Http_Response HTTP response
	 */
	abstract public function perform_get_request($url, $timeout = false);

	/**
	 * Perform a HEAD HTTP operation with an optional timeout and return the response
	 * headers - NOTE: head requests have no response body
	 *
	 * @param string $url
	 * @param float $timeout
	 * @return Http_Response HTTP response
	 */
	abstract public function perform_head_request($url, $timeout = false);

	/**
	 * Perform a POST HTTP operation with an optional timeout and return the response
	 * contents, use getLastResponseHeaders to retrieve HTTP headers
	 *
	 * @param string $url
	 * @param string $rawPost
	 * @param string $contentType
	 * @param float $timeout
	 * @return Http_Response HTTP response
	 */
	abstract public function perform_post_request($url, $raw_post, $content_type, $timeout = false);

}